package com.jia.together.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jia.together.entity.Banner;
import com.jia.together.service.BannerService;
import com.jia.together.utils.R;
import com.jia.together.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/front/banner")
//@CrossOrigin
public class BannerFrontController {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private BannerService bannerService;

    @GetMapping("/list")
    public R list(){
        Object bannerList = redisUtil.get("BANNER_LIST");
        if (bannerList == null){
            QueryWrapper<Banner> wrapper = new QueryWrapper<>();
            wrapper.orderByAsc("sort");
            wrapper.last("limit 3");
            List<Banner> list = bannerService.list(wrapper);
            redisUtil.set("BANNER_LIST",list,60*60);
            return R.ok().data("data",list);
        }
        return R.ok().data("data",(List<Banner>) bannerList);
    }
}
